var  client = require('../database');
mysql = client.getDbCon();

//获取应用详细列表数据
exports.getAppDetails = function(req,res){
    var selectNameSQL = "select * from tb_appdetails ";

    mysql.query(selectNameSQL,function(err,rows){
        if (err) console.log(err);
        console.log("SELECT ==> ");
        console.log(rows);
        if(rows){
            var data = JSON.stringify(rows);
            return res.render('appdetails',{app_details:data});
        }
    });
};

//增加一条App列表数据
exports.createAppDetails = function(req,res){
    var projectname = req.body.projectname;
    var domainname = req.body.domainname;
    var status = req.body.status;
    var remark = req.body.remark;

    var insertSQL = 'insert into tb_appdetails values(null,' +'"'+ projectname +'"' +',' + '"'+domainname+'"' +',' + '"'+status+'"' + ',' +'"'+remark+'"'+')';
    mysql.query(insertSQL,function(err,rows){
        if (err) console.log(err);
        console.log("SELECT ==> ");
        console.log(rows);
        if(rows.protocol41){
            return res.redirect('/getAppDetails');
        }
    });
};

exports.editAppDetails = function(req,res){
    var projectname = req.body.projectname;
    var id = req.body.id;
    var status = req.body.status;
    var remark = req.body.remark;
    var domainname = req.body.domainname;

    var updateSQL = 'update tb_appdetails set projectname=' +'"'+ projectname +'"' +',' +'domainname='+ '"'+domainname+'"' +',' +'status='+ '"'+status+'"'+',' +'remark='+ '"'+remark+'"'+'where id='+'"'+ id +'"';

    mysql.query(updateSQL,function(err,rows){
        if (err) console.log(err);
        console.log("UPDATE ==> ");
        console.log(rows);
        if(rows.protocol41){
            return res.redirect('/getAppDetails');
        }
    });
};
exports.deleteAppDetails = function(req,res){
    var id = req.query['id'];
    var deleteSQL = 'delete from tb_appdetails  where id=' +'"'+ id +'"' ;
    mysql.query(deleteSQL,function(err,rows){
        if (err) console.log(err);
        console.log("DELETE ==> ");
        console.log(rows);
        if(rows.protocol41){
            return res.redirect('/getAppDetails');
        }
    });
};
